package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.IPage;
import org.apache.tapestry.valid.ValidationDelegate;
import com.javaforge.tapestry.spring.annotations.InjectSpring;
import com.crm.service.IContactService;
import com.crm.model.User;
import com.crm.model.Contact;
import com.crm.model.Customer;

import java.util.List;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class ContactList extends BasePage {

    private int count;

    @InjectSpring("contactService")
    public abstract IContactService getContactServ();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectPage("ContactNew")
    public abstract ContactNew getContactNewPage();

    @InjectPage("ContactModify")
    public abstract ContactModify getContactModifyPage();

    @InjectPage("ContactOfCustomer")
    public abstract ContactOfCustomer getContactOfCustomerPage();

    @Bean
    public abstract ValidationDelegate getDelegate();

    public abstract String getKeyword();

    public abstract String getFieldSearch();

    @Persist
    public abstract Customer getCustomer();

    public abstract void setCustomer(Customer customer);

    public String getRecordCount() {
        return "共" + count + "条记录";
    }

    public List getEntries() {
        try {
            List list = getContactServ().getContactByCustomerId(getUserSession(), getCustomer().getId());
            count = list.size();
            return list;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public IPage newContact() {
        ContactNew page = getContactNewPage();
        page.setCustomer(getCustomer());
        return page;
    }

    public IPage goback() {
        return getContactOfCustomerPage();
    }

    public IPage onShowDetails(Contact contact) {
        ContactModify page = getContactModifyPage();
        page.setContactInfo(contact);
        page.setSourcePage(this.getPageName());
        return page;
    }

    public IPage deleteContact(Contact contact) {
        getContactServ().delete(contact, getUserSession());
        return this;
    }
}
